package com.aaa.backsystem.controller;

import com.aaa.backsystem.aop.SaveOrUpdateEntityAnn;
import com.aaa.backsystem.entity.*;
import com.aaa.backsystem.service.DeptService;
import com.aaa.backsystem.service.PetrolStationService;
import com.aaa.backsystem.service.UserRoleService;
import com.aaa.backsystem.service.UserService;
import com.aaa.backsystem.shiro.ShiroUtil;
import com.aaa.backsystem.util.MyConstants;
import com.aaa.backsystem.util.TreeUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * fileName:SignInController
 * description:
 * author:gyc
 * createTime:2020/7/23 21:32
 * version:1.0.0
 */
@Controller
@RequestMapping("SignIn")
public class SignInController extends BaseController {

    @Autowired
    private PetrolStationService petrolStationService;

    @Autowired
    private DeptService deptService;

    @Autowired
    private UserService userService;

    @Autowired
    private UserRoleService userRoleService;

    @RequestMapping("toAddCompany")
    public String toAddCompany(){
        return "addCompany";
    }

    @RequestMapping("toAddUser")
    public String toAddUser(){
        return "addUser";
    }

    /**
     * 公安局树
     * @return
     */
    @ResponseBody
    @RequestMapping("findDept")
    public List<TreeNode> findDept(){
        List<Dept> deptList = deptService.selectList(null);
        List<TreeNode> treeNodes = new ArrayList<>();
        for (Dept dept : deptList) {
            Integer id = dept.getDeptId();
            Integer pid = dept.getParentId();
            String title = dept.getDeptName();
            treeNodes.add(new TreeNode(id,pid,title));
        }
        return TreeUtil.layerTree(treeNodes);
    }

    /**
     * 注册企业（添加加油站信息）
     * @param petrolStation
     * @return
     */
    @RequestMapping("/addPetrolStation")
    @ResponseBody
    public Result addPetrolStation(PetrolStation petrolStation) {
        Integer tankerAmount = petrolStation.getTankerAmount();
        if (tankerAmount<= MyConstants.PS_MIN_NUMBER){
            petrolStation.setPertolStationTypeId(1);
        }else if (tankerAmount>MyConstants.PS_MIN_NUMBER&&tankerAmount<=MyConstants.PS_MIDDLE_NUMBER){
            petrolStation.setPertolStationTypeId(2);
        }else if (tankerAmount>MyConstants.PS_MIDDLE_NUMBER){
            petrolStation.setPertolStationTypeId(3);
        }
        boolean insert = petrolStationService.insert(petrolStation);
        if (insert) {
            return success();
        } else {
            return error();
        }
    }

    /**
     * 通过派出所id查找对应加油站信息到下拉框
     * @param deptId
     * @param model
     * @return
     */
    @ResponseBody
    @RequestMapping("findPetrolStationByDeptId")
    public List<PetrolStation> findPetrolStationByDeptId(@RequestParam String deptId, Model model){
        //System.out.println(deptId);
        Wrapper<PetrolStation> wrapper = new EntityWrapper<>();
        wrapper.eq("dept_id",Integer.valueOf(deptId));
        wrapper.eq("status",0);
        List<PetrolStation> petrolStationList = petrolStationService.selectList(wrapper);
        System.out.println(petrolStationList);
        return petrolStationList;
    }

    /**
     * 选择加油站传入id 返回加油站信息
     * @param petrolStationId
     * @return
     */
    @ResponseBody
    @RequestMapping("findPetrolStationById")
    public Result findPetrolStationById(String petrolStationId){
        if (petrolStationId!=null&&petrolStationId!=""){
            Wrapper<PetrolStation> wrapper = new EntityWrapper<>();
            wrapper.eq("petrol_station_id",Integer.valueOf(petrolStationId));
            PetrolStation petrolStation = petrolStationService.selectList(wrapper).get(0);
            //System.out.println(petrolStation);
            if (petrolStation!=null){
                return success(petrolStation);
            }else {
                return error();
            }
        }
        return null;
    }

    /**
     * 注册用户（添加用户信息）
     * @param user
     * @return
     */
    @SaveOrUpdateEntityAnn(entityClass = User.class)
    @RequestMapping("/addUser")
    @ResponseBody
    public Result addUser(User user) {
        //获取明文密码
        String password = user.getPassword();
        //生成随机盐值
        String salt = UUID.randomUUID().toString();
        //加密加盐
        String encryption = ShiroUtil.encryptionBySalt(salt, password);
        //存入密码和盐值
        user.setPassword(encryption);
        user.setSalt(salt);
        //存入数据库
        boolean insert = userService.insert(user);
        //默认将注册用户设为加油站法人角色
        userRoleService.insert(new UserRole(user.getUserId(),6));
        if (insert) {
            return success();
        } else {
            return error();
        }
    }
}
